﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using COM.OA.BLL;
using System.Collections.Generic;
using COM.OA.Entity;

public partial class gw_senddocument : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    //连动
    protected void ddldepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        //取部门的值
        string ddldepartment = this.ddldepartment.Text;

        SqlParameter parambumen = new SqlParameter("@deptid", ddldepartment);
        IList<liandongren> lists = BLLGeneric.Select<liandongren>(CommandType.StoredProcedure, "liandong", parambumen);
        this.ddlname.DataSource = lists;
        this.ddlname.DataTextField = "Em_name";
        this.ddlname.DataValueField = "Em_name";
        this.ddlname.DataBind();
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        //接收人
        string takeuser = this.tboxperson.Text;
        //判断接收人是否为空
        if (takeuser == null || takeuser == "")
        {
            this.Response.Write(string.Format(GetRedirect.WINALERT,"接收人不能为空"));
            return;
        }
        //部门
        string dept = this.tboxdept.Text;
        //判断接收部门是否为空
        if (dept == null || dept == "")
        {
            this.Response.Write(string.Format(GetRedirect.WINALERT, "请选择接收部门！！"));
            return;
        }
        //标题
        string title = this.txttitle.Text;
        //判断标题是否为空
        if (title == null || title == "")
        {
            this.Response.Write(string.Format(GetRedirect.WINALERT, "请输入标题！！"));
            return;
        }
        //判断上传文件时候为空
        if (string.IsNullOrEmpty(this.fileUpload1.PostedFile.FileName)) 
        {
            this.Response.Write(string.Format(GetRedirect.WINALERT, "请选择你要发送的公文！"));
            return;
        }
        //接收人id
        int takeuserid = 0;

        string sql = "em_name='{0}'";
        sql = string.Format(sql, takeuser);
        IList<employee> elist = employeeBLL.Select(sql);
        if (elist.Count == 1)
        {
            foreach (employee em in elist)
            {
                takeuserid = em.em_id;
            }
            string uid = "u_em_id={0}";
            uid = string.Format(uid,takeuserid);
            IList<users> u = usersBLL.Select(uid);
            if (u.Count == 1)
            {
                foreach(users user in u)
                {
                    takeuserid = user.u_id;
                }
            }
        }

        //部门id
        int deptid = 0;

        string sql2 = "dept_department='{0}'";
        sql2 = string.Format(sql2, dept);
        IList<department> deptlist = departmentBLL.Select(sql2);
        if (deptlist.Count == 1)
        {
            foreach (department d in deptlist)
            {
                deptid = d.dept_id;
            }
        }
      
        //附件
        string gwpath = this.Server.MapPath("gwupload") + @"\" + this.fileUpload1.FileName;

        //将文件保存在指定的路径
        this.fileUpload1.SaveAs(gwpath);

        //附件路径
        string annexpath = "gwupload/" + this.fileUpload1.FileName;

        //发送人
        string username = this.Request.Form["txtusername"];

        //发送人id
        int userid = 0;

        string sql3 = "u_username='{0}'";
        sql3 = string.Format(sql3, username);
        IList<users> userlist = usersBLL.Select(sql3);
        if (userlist.Count == 1)
        {
            foreach (users u in userlist)
            {
                userid = u.u_id;
            }
        }

        //发送时间
        DateTime sendtime = DateTime.Parse(this.Request.Form["txtsendtime"]);

        //下载次数
        int download = 0;

        //阅读次数
        int reads = 0;

        //点击次数
        int clicks = 0;

        //群组id
        int gid = 1;

        //将数据存进实体
        document docu = new document();
        docu.doc_dept_id = deptid;//部门
        docu.doc_u_id=takeuserid;//接收人id
        docu.doc_title=title;//标题
        docu.doc_annex = annexpath;//附件
        docu.doc_downloads=download;//下载次数
        docu.doc_reads=reads;//阅读次数
        docu.doc_clicks=clicks;//点击次数
        docu.doc_releasetime=sendtime;//发送时间
        docu.doc_sendu_id=userid;//发送人id
        docu.doc_g_id = gid;//群组id

        //执行插入，判断返回值
        int flag = documentBLL.Insert(docu);
        if (flag == 0)
        {
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('公文发送失败！！');</script>");
        }
        this.Response.Redirect("gw_success.aspx");
    }
  
}
